<?php

use yii\db\Schema;
use yii\helpers\Security;
use yii\db\Expression;

class m140329_133820_user_init extends \yii\db\Migration {

    public function up() {

        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT = "用户信息" AUTO_INCREMENT = 10000';
        }
        $this->createTable('user_user', [
            'id' => Schema::TYPE_PK,
            'username' => Schema::TYPE_STRING . '(20) NOT NULL  COMMENT "用户名"',
            'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL COMMENT "身份码"',
            'password_hash' => Schema::TYPE_STRING . ' NOT NULL COMMENT "密码"',
            'password_reset_token' => Schema::TYPE_STRING . '(32) COMMENT "密码重置令牌"',
            'access_token' => Schema::TYPE_STRING . '(32) COMMENT "访问令牌"',
            'email' => Schema::TYPE_STRING . ' COMMENT "邮箱"',
            'mobile' => Schema::TYPE_STRING . ' COMMENT "手机号码"',
            'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 1 COMMENT "状态"',
            'role_id' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10000 COMMENT "角色编号"',
            'create_time' => Schema::TYPE_DATETIME . ' NOT NULL COMMENT "创建时间"',
            'update_time' => Schema::TYPE_DATETIME . ' NOT NULL COMMENT "更新时间"',
            'active_code' => Schema::TYPE_DATETIME . ' COMMENT "激活码"',
            'active_time' => Schema::TYPE_DATETIME . ' COMMENT "激活时间"',
            'last_login_time' => Schema::TYPE_DATETIME . ' COMMENT "最后登录时间"',
            'last_login_ip' => Schema::TYPE_STRING . ' COMMENT "最后登录IP"',
            'UNIQUE KEY `username` (`username`)',
            'KEY `status` (`status`)',
            'KEY `role_id` (`role_id`)',
            'KEY `email` (`email`)',
            'KEY `mobile` (`mobile`)',
            'KEY `access_token` (`access_token`)',
            'KEY `auth_key` (`auth_key`)',
                ], $tableOptions);
        $this->insert('user_user', [
            'username' => 'admin',
            'auth_key' => Security::generateRandomKey(),
            'password_hash' => Security::generatePasswordHash('admin'),
            'password_reset_token' => Security::generateRandomKey(),
            'access_token' => Security::generateRandomKey(),
            'email' => 'admin@localhsot',
            'mobile' => '88888888',
            'status' => '1',
            'role_id' => '10000',
            'create_time' => new Expression('NOW()'),
            'update_time' => new Expression('NOW()'),
            'active_time' => new Expression('NOW()'),
        ]);
        $this->insert('user_user', [
            'username' => 'demo',
            'auth_key' => Security::generateRandomKey(),
            'password_hash' => Security::generatePasswordHash('demo'),
            'password_reset_token' => Security::generateRandomKey(),
            'access_token' => Security::generateRandomKey(),
            'email' => 'admin@localhsot',
            'mobile' => '88888888',
            'status' => '1',
            'role_id' => '10',
            'create_time' => new Expression('NOW()'),
            'update_time' => new Expression('NOW()'),
            'active_time' => new Expression('NOW()'),
        ]);
        return true;
    }

    public function down() {
        $this->dropTable('user_user');
        return true;
    }

}
